package 线程;

public class 继承Tread创建线程 {
    public static void main(String[] args) {
        System.out.println("主线程111");
        //使用继承Thread,创建线程,Cat类重写了Thread的run方法
        Cat cat = new Cat();
        cat.start();

        //使用实现Runnable创建线程，Dog类实现了Runnable接口的run方法
//        Thread thread = new Thread(new Dog());
//        thread.start();
        System.out.println(Thread.currentThread().getName());
    }
}

class Cat extends Thread{
    private int times;
    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(1000);
                System.out.println("小狗" + Thread.currentThread().getName());
                times +=1;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (times == 100) {
                break;
            }
        }

    }
}
class Dog implements Runnable{
    private int times;
    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(1000);
                System.out.println("hello\t" + Thread.currentThread().getName());
                times +=1;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (times == 8) {
                break;
            }
        }
    }
}
